package com.novabluetooth.services.servicedevices;

import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.util.Log;

import com.health.tools.ArithmeticUtils;
import com.health.tools.GlobalConts;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.services.TJBluetoothLeService;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.UUID;

/**
 * 百捷三合一
 *
 * @author nova
 */
public class TripleService extends TJBluetoothLeService {
    /**
     * 三合一
     */
    public final static UUID serviceTripleUUID = UUID.fromString("00001000-0000-1000-8000-00805F9B34FB");
    public final static UUID readCharactTripleUUID = UUID.fromString("00001002-0000-1000-8000-00805F9B34FB");

    @Override
    public void servicesDiscovered() {
        super.servicesDiscovered();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            BluetoothGattService gattService = mBluetoothGatt.getService(serviceTripleUUID);
            readGattCharact = gattService.getCharacteristic(readCharactTripleUUID);
            mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
        }
    }

    @Override
    public boolean isRightData(byte[] buffer) {
        super.isRightData(buffer);
        int readSize_xy = buffer.length;
        Log.e("isRightData", "三合一接收到的数据信息为：" + byteArray2HexString(buffer, readSize_xy));
        String signFlag = byteArray2HexString(new byte[] { buffer[4] }, new byte[] { buffer[4] }.length);
        String s = byteArray2HexString(buffer, buffer.length);
        Log.e("isRightData11", "datass：" +s.substring(36, 38)+"\n"+s.substring(34, 36));
        int data1 = Integer.parseInt(s.substring(36, 38) + "00", 16);//高位补00
        int data2 = Integer.parseInt(s.substring(34, 36), 16);
        int data = data1 + data2;
        Log.e("isRightData11", "data：" +data);

        if(BluetoothConstant.TRIPLE_DATA_GLUC.equals(signFlag)){
            //41代表血糖,血糖的单位换算：mg/dl÷18=mmol/L
            //24 50 43 4C 41 00 00 00 09 00 00 00 0D 01 01 05 0E 78 00 C3
            Log.e("data222", signFlag+"data：" +((double) data)/18);
            checkData=signFlag+";"+String.valueOf(ArithmeticUtils.round1Db(((double) data)/18));
        }else if(BluetoothConstant.TRIPLE_DATA_UA.equals(signFlag)){
            //51代表尿酸，尿酸的单位换算： （mg/dl）*0.1÷16.81 mmol/L
            // 24 50 43 4C 51 00 00 00 09 00 00 00 0D 0C 03 0F 31 04 00 99
            Log.e("data222", signFlag+"data：" +((double) data)*0.1/16.81);
            checkData=signFlag+";"+String.valueOf(ArithmeticUtils.round2Db(((double) data)*0.1/16.81));

        }else if(BluetoothConstant.TRIPLE_DATA_CHOL.equals(signFlag)){
            //61代表胆固醇，胆固醇的单位换算：（mg/dl）÷ 38.66 =（mmol/L).
            //24 50 43 4C 61 00 00 00 09 00 00 00 0D 01 01 05 29 79 00 FF
            Log.e("data222", signFlag+"data：" +((double) data)/38.66 );
            checkData=signFlag+";"+ String.valueOf(ArithmeticUtils.round2Db(((double) data)/38.66 ));
        }else if(BluetoothConstant.TRIPLE_DATA_HB.equals(signFlag)){
            //E1代表 血红蛋白   g/dL *0.1;    mmol/L  Value * 0.1*(1/1.61)
            //24 50 43 4C E1 00 00 00 09 00 00 00 12 01 0B 04 0F 02 01 FD
            Log.e("dataHB", signFlag+"data：" + data*0.1);
            //部分整数*0.1会出现 精度丢失问题
            DecimalFormat df = new DecimalFormat("#.0");
            df.setRoundingMode(RoundingMode.FLOOR);//此舍入模式始终不会增加计算值。
            checkData=signFlag+";"+  df.format(data*0.1);
        }

        Log.e("BluetoothLeService", "三合一出结果" + checkData);
        ifSuccess = true;
        disconnect();
        return ifSuccess;

    }


    @Override
    public void closeDevice() {
        super.closeDevice();
    }


}
